package com.cyk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyk.reggie.common.BaseContext;
import com.cyk.reggie.common.R;
import com.cyk.reggie.dto.OrdersDto;
import com.cyk.reggie.entity.Category;
import com.cyk.reggie.entity.OrderDetail;
import com.cyk.reggie.entity.Orders;
import com.cyk.reggie.entity.Setmeal;
import com.cyk.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders.toString());
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 分页查询订单信息
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime){
        log.info("page = {},pageSize = {}，number = {}",page, pageSize, number);
        if (beginTime != null && endTime != null){
            log.info(", beginTime = {},endTime={}",beginTime,endTime);
        }
        //分页构造器
        Page<Orders> pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加查询条件，根据number进行like模糊查询,下单时间范围在beginTime与endTime之间
        queryWrapper.like(number != null, Orders::getNumber, number)
                .gt(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime)
                .lt(StringUtils.isNotEmpty(endTime), Orders::getOrderTime, endTime);
        //添加排序条件，根据下单时间进行排序
        queryWrapper.orderByAsc(Orders::getOrderTime);
        //进行分页查询
        orderService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 分页查询订单信息
     * @param page
     * @param pageSize
     * @return
     */
/*    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        log.info("page = {},pageSize = {}",page, pageSize);
        //分页构造器
        Page<Orders> pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加排序条件，根据下单时间进行排序
        queryWrapper.orderByAsc(Orders::getOrderTime);
        //进行分页查询
        orderService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }*/

    /**
     * 查询用户页面的最新订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        //分页构造器对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> pageDto = new Page<>(page,pageSize);
        //构造条件查询对象
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //这里是直接把分页的全部结果查询出来，没有分页条件
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Orders::getOrderTime);
        this.orderService.page(pageInfo,queryWrapper);

        //通过OrderId查询对应的OrderDetail
        //LambdaQueryWrapper<OrderDetail> queryWrapper2 = new LambdaQueryWrapper<>();

        //对OrderDto进行需要的属性赋值
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> orderDtoList = records.stream().map((item) ->{
            OrdersDto orderDto = new OrdersDto();
            //此时的orderDto对象里面orderDetails属性还是空 下面准备为它赋值
            Long orderId = item.getId();//获取订单id
            List<OrderDetail> orderDetailList = this.orderService.getOrderDetailsByOrderId(orderId);
            BeanUtils.copyProperties(item,orderDto);
            //对orderDto进行OrderDetails属性的赋值
            orderDto.setOrderDetails(orderDetailList);
            return orderDto;
        }).collect(Collectors.toList());

        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        pageDto.setRecords(orderDtoList);
        return R.success(pageDto);
    }

    /**
     * 修改订单状态
     * @return
     */
    @PutMapping
    public R<String> updateOrderStatus(@RequestBody Orders orders){
        log.info("订单信息={}", orders);
        //前端传过来订单号和修改后的状态status
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(orders.getId() != null, Orders::getId, orders.getId());

        orderService.updateById(orders);

        return R.success("修改订单状态成功");
    }
}
